package com.hpe.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.fileupload.FileItem;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
 任浩雷
 */
public class UploadExcel {

	public List<String> uploadexcel(FileItem item){
		
		List<String> list1 = new ArrayList<String>();
		String rowsvalues = "";
		if (item.getName().endsWith(".xls")||item.getName().endsWith(".xlsx")) {
            // 说明是文件,不过这里最好限制一下
			try {
				HSSFWorkbook workbook = new HSSFWorkbook(item.getInputStream());
		
        	//读取默认第一个工作表sheet
    		HSSFSheet sheet = workbook.getSheetAt(0);
    		int firstRowNum = 1;
    		//获取sheet中最后一行行号
    		int lastRowNum = sheet.getLastRowNum();
    		for(int ii = firstRowNum;ii<=lastRowNum;ii++){
    			HSSFRow row = sheet.getRow(ii);
    			//获取当前行最后单元格列号
    			int lastCellNum = row.getLastCellNum();
    			for(int j = 0;j<lastCellNum;j++){
    				HSSFCell cell = row.getCell(j);
    				String value = cell.getStringCellValue();
    				if(j==0){
    					rowsvalues=value;
    				}
    				else{
    					rowsvalues = rowsvalues+","+value;
    				}
    			}
    			
    			list1.add(rowsvalues);
    			
    		}
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		return list1;
}
}
